package edu.cornell.cs4740.postag.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.cornell.cs4740.postag.parsing.Sentence;
import edu.cornell.cs4740.postag.parsing.WordContainer;

public class Utilities {
	
	public static final String NULL_POS = "__NA__";
	public static final String NULL_WORD = "__NA__";
	
	private static Map<String, Integer> wordIndexMap = new HashMap<String, Integer>();
	
	private static Map<Integer, String> indexPosMap = new HashMap<Integer, String>();
	private static Map<String, Integer> posIndexMap = new HashMap<String, Integer>();
	
	
	public static void add_word_index_pair(String word, int index) {
		wordIndexMap.put(word, new Integer(index));
	}
	
	public static void addIndexPOSPair(int index, String pos) {
		indexPosMap.put(new Integer(index), pos);
		posIndexMap.put(pos, new Integer(index));
	}
	
	public static int word_to_index(String word){
	  
	  return (wordIndexMap.containsKey(word) ? wordIndexMap.get(word).intValue() : -1);

    }
	
	public static int getNumWords() {
		return wordIndexMap.size();
	}
	
	public static int getNumPos() {
		return indexPosMap.size();
	}
	

	public static int getIndexForPos(String pos) {
		return (posIndexMap.containsKey(pos) ? posIndexMap.get(pos).intValue() : -1);
	}

	public static String index_to_pos(int i) {
		 return (indexPosMap.containsKey(new Integer(i)) ? indexPosMap.get(new Integer(i)) : null);
	}

	/* number correct / total */
	public static double computeAccuracy(List<Sentence> predictions, List<Sentence> actual) {
		if (predictions.size() != actual.size()) {
			System.out.println("ERROR: Prediction and actual lists are not the same size");
			return 0.;
		}

		double totalCorrect = 0.;
		double totalWords = 0.;

		for (int i = 0; i < predictions.size(); i++) {
			Sentence predictedSentence = predictions.get(i);
			Sentence actualSentence = actual.get(i);

			if (predictedSentence.getLength() != actualSentence.getLength()) {
				System.out.println("ERROR: Prediction sentence and actual sentence length are not equal for sentence "
						+ i);
			}

			for (int j = 0; j < predictedSentence.getLength(); j++) {
				WordContainer predictedWord = predictedSentence.getWord(j);
				WordContainer actualWord = actualSentence.getWord(j);

				String predictedPos = predictedWord.getPos();
				String actualPos = actualWord.getPos();
				if (predictedPos.equals(actualPos)) {
					totalCorrect++;
				}

				totalWords++;
			}
		}

		return totalCorrect / totalWords;
	}
}
